package com.thinkit.bigdata.web.dao.tagging;

import com.alibaba.fastjson.JSON;
import com.thinkit.bigdata.core.ConfigHelper;
import com.thinkit.bigdata.core.util.TaskUtil;
import com.thinkit.bigdata.web.coremet.SystemStatic;
import com.thinkit.bigdata.web.model.es.audioEs.VoiceInfoDto;
import com.thinkit.bigdata.web.model.result.HtmlResult;
import com.thinkit.bigdata.web.model.tagging.TaggingSource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.log4j.Logger;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaggingDao {

    private static Logger logger = Logger.getLogger(TaggingDao.class);

    private List<TaggingSource> strs2Entitys(List<String> docList) {
        List<TaggingSource> sources = new ArrayList<>();
        for (String doc : docList) {
            sources.add(str2entity(doc));
        }
        return sources;
    }

    public List<VoiceInfoDto> formatVoice(List<TaggingSource> docList) throws InvocationTargetException, IllegalAccessException {
        List<VoiceInfoDto> voiceInfos = new ArrayList<>();
        for (TaggingSource source : docList) {
            VoiceInfoDto dto = new VoiceInfoDto();
            ConvertUtils.register(new DateConverter(null), java.util.Date.class);
            BeanUtils.copyProperties(dto, source.getVoiceInfo());
            dto.setSerialNumber(source.getSerialNumber());
            voiceInfos.add(dto);
        }
        return voiceInfos;
    }

    private TaggingSource str2entity(String doc) {
        return JSON.parseObject(doc, TaggingSource.class);
    }

    public TaggingSource getSourceBySerialNumber(String serialNumber) {
        String mgs = SystemStatic.crud.get(SystemStatic.STATISTICS_ES_INDEX, SystemStatic.STATISTICS_ES_TYPE, serialNumber);
        return this.str2entity(mgs);
    }

    public HtmlResult updateSource(TaggingSource source) {
        RestStatus status = SystemStatic.crud.updateIndexRefreshByNow(SystemStatic.STATISTICS_ES_INDEX, SystemStatic.STATISTICS_ES_TYPE, JSON.toJSONString(source), source.getSerialNumber());
        if (200 == status.getStatus()) {
            return HtmlResult.success("数据操作成功");
        }
        logger.info("数据更新失败,状态码为【" + status.getStatus() + "】");
        return HtmlResult.failure("数据更新失败");
    }

    public List<TaggingSource> getTaggingList(BoolQueryBuilder boolQuery, String[] includes, Integer pageNum, Integer pageSize) {
        List<TaggingSource> sourceList = new ArrayList<>();
        //总条数
        Integer sum = SystemStatic.crud.queryQBuilderAllByMes(SystemStatic.STATISTICS_ES_INDEX, SystemStatic.STATISTICS_ES_TYPE, boolQuery);
        //单词查询条数
        int excleMax = ConfigHelper.getInt("export.data.sheet.max.number", 1000);
        List<Integer> pageTout = TaskUtil.formatSum(sum, excleMax);//todo最后一个数字需要特殊处理:6022
        if (pageNum != null) {
            pageTout.clear();
            pageTout.add(pageNum);
            excleMax = pageSize;
        }
        for (Integer aPageTout : pageTout) {
            List<String> docList = SystemStatic.crud.queryQBuilderByMes(
                    SystemStatic.STATISTICS_ES_INDEX,
                    SystemStatic.STATISTICS_ES_TYPE,
                    aPageTout,
                    excleMax,
                    boolQuery,
                    includes
            );
            if (docList.isEmpty()) continue;
            sourceList.addAll(this.strs2Entitys(docList));
        }
        return sourceList;
    }
}
